package gs.wicket.app.register;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.value.ValueMap;
import org.modelibra.wicket.view.View;

import gs.wicket.app.session.GSSession;
import gs.wicket.app.base.*;
import gs.wicket.app.account.*;
import gs.wicket.app.index.*;
import gs.wicket.app.login.*;

public class RegisterPage extends BasePage {

	private TextField imeTextField;
	private TextField usernameTextField;
	private PasswordTextField passwordTextField;
	private TextField ulicaTF;
	private TextField gradTF;
	private TextField postanskiBrojTF;
	private TextField emailTF;
	
	private String username;
	private String password;
	private String ime;
	private String ulica;
	private String grad;
	private String postanskiBroj;
	private String email;

	private Korisnik k = new Korisnik(null, null, null, null, null, null,
			null);

	List<String> spol = Arrays.asList("Musko", "Zensko", "Bez odgovora");

	public RegisterPage() {

		List<String> vrste = Arrays.asList("Musko", "Zensko", "Bez odgovora");
		passwordTextField = new PasswordTextField("password", new Model(""));


		imeTextField = new RequiredTextField("ime", new PropertyModel(k, "ime"));

		usernameTextField = new RequiredTextField("username", new PropertyModel(k, "username"));

		passwordTextField.setRequired(true);
        
		ulicaTF = new TextField("ulica",new PropertyModel(k, "ulica"));
		postanskiBrojTF= new TextField("postanskibroj",new PropertyModel(k, "postanskiBroj"));
		gradTF=new TextField("grad",new PropertyModel(k, "grad"));
		emailTF= new TextField("email",new PropertyModel(k, "email"));
		CompoundPropertyModel model = new CompoundPropertyModel(new ValueMap());

		Form form = new Form("form", model);
		add(form);

		form.add(imeTextField);
		form.add(usernameTextField);
		form.add(passwordTextField);

		form.add(ulicaTF);
		form.add(postanskiBrojTF);
		form.add(gradTF);
		form.add(emailTF);
		form.add(new Label("register.page.name", getLangProperty("register.page.name")));
		form.add(new Label("register.page.username", getLangProperty("register.page.username")));
		form.add(new Label("register.page.password", getLangProperty("register.page.password")));
		form.add(new Label("register.page.street", getLangProperty("register.page.street")));
		form.add(new Label("register.page.city", getLangProperty("register.page.city")));
		form.add(new Label("register.page.zip", getLangProperty("register.page.zip")));
		form.add(new Label("register.page.sex", getLangProperty("register.page.sex")));
		form.add(new Label("register.page.terms", getLangProperty("register.page.terms")));

		form.add(new ListChoice("spol", new Model(""), spol));
		form.add(new TextArea("textarea", new Model("Registracijom na ovu stranicu\n" + "prihvatam sve obaveze i duznosti korisnika")).setRequired(true));
		form.add(new Link("cancel") {
			
			private static final long serialVersionUID = 1L;
			{
				add(new AttributeModifier("value", new Model(getLangProperty("konvertor.cancel"))));
			}

			@Override
			public void onClick() {
				//getRequestCycle().setRedirect(false);
				setResponsePage(LogInPage.class);
			}
		});
		form.add(new Button("add") {

			private static final long serialVersionUID = 1L;
			{
				add(new AttributeModifier("value", new Model(getLangProperty("register.page.register"))));
			}
			@Override
			public void onSubmit() {
				ime = imeTextField.getValue();
				username = usernameTextField.getValue();
				password = passwordTextField.getValue();
				ulica=ulicaTF.getValue();
				grad=gradTF.getValue();
				postanskiBroj=postanskiBrojTF.getValue();
				email=emailTF.getValue();
				
				Korisnik korisnik = new Korisnik(username, password, ime,
						ulica, grad, postanskiBroj, email);
				ArrayList<Korisnik> korisnici = ((GSSession) getSession())
						.getKorisnici();
				korisnici.add(korisnik);

				GSSession gsSession = (GSSession) getSession();
				gsSession.setLogovanKorisnik(korisnik);
				setResponsePage(AccountPage.class);
			}
		});

	}
}
